package com.pc.ipushpin.common.manager;

import com.pc.ipushpin.business.model.Label;
import com.pc.ipushpin.business.model.Store;
import com.pc.ipushpin.business.service.ILabelService;
import com.pc.ipushpin.business.service.IStoreService;
import com.pc.ipushpin.common.util.SignValidator;
import com.pc.ipushpin.common.util.StwConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.*;

@Service
@Transactional
public class StoreModuleManager {

    @Resource
    private IStoreService storeService;

    private static int pageSize = StwConstant.pageSize;

    private static String imgService = StwConstant.FILE_SERVER_PATH;
    private static String tomcatPath = "/"+ StwConstant.TOMCAT_ROOT;

    private static Logger logger = LoggerFactory.getLogger(StoreModuleManager.class);

    public Map<String,Object> findAllByLabelIds(Map<String, String> mapIn, HttpServletRequest request) {
        logger.info("findAllByLabelIds:"+mapIn);

        Map<String,Object> result = new HashMap<>();

        String labelIds = mapIn.get("labelIds");
        mapIn.put("labelIds", labelIds);
        if(!"00".equals(SignValidator.checkEmpty(mapIn).get("resultCode"))){
            return SignValidator.checkEmpty(mapIn);
        }
        List<String> labels = Arrays.asList(labelIds.split(","));
        List<Store> stores = storeService.findAllByLabelIds(labels);
        if(stores != null && stores.size() > 0) {
            List<Map<String,Object>> storeMaps = new ArrayList<>();
            for (Store store:stores) {
                Map<String,Object> storeMap = new HashMap<>();
                storeMap.put("name",store.getName());
                storeMap.put("id",store.getId());
                storeMap.put("telephone",store.getTelephone());
                storeMap.put("address",store.getAddress());
                storeMap.put("longitude",store.getLongitude());
                storeMap.put("latitude",store.getLatitude());
                storeMap.put("brand",store.getBrand());
                storeMaps.add(storeMap);
            }
            result.put("data",storeMaps);
            result.put("code",200);
        } else {
            result.put("code",400);
        }
        logger.info("findAllByLabelIds result:"+result);
        return result;
    }
}
